
<?php
/**
 * @authors alex (20779512@qq.com)
 * @date    2014-03-24 17:25:39
 * @version 1.0
 */

class UserAction extends AdminAction {
    
    #列表
    public function index(){ 
        $role = M('Role')->getField('id,name');
        $obj = M('Admin');
        $list = $obj->order('id asc')->select();
        foreach ($list as $key => $value) {
            $cityStr = '';
            $cityArr = explode(',',$value['city']);
            foreach ($cityArr as $k => $val) {
                $city = M('City')->where('id='.$cityArr[$k])->getField('name');
                $cityStr .= $city . '&nbsp;';                
            }
            $list[$key]['city'] = $cityStr;
        }
        $this->assign('list', $list);   
        $this->assign('role', $role);    
    	$this->display();
    }

    #添加
    public function add(){
        $obj = M('City');
        $this->assign('city',$obj->select());
        $obj = M('Role');
        $this->assign('group',$obj->select());
    	$this->display();
    }

    #保存
    public function save(){
        $obj = D('Admin');

        if ($data = $obj->create()) {
            if ($list=$obj->add($data)) {

                $obj = M('roleUser');
                $tmp['role_id']=$data['group'];
                $tmp['user_id']=$list;
                $obj->add($tmp);

                $state = 'SUCCESS';
                $message = '管理员添加成功';
                $url = U('User/index');
            } else {
                $state = '管理员添加失败';
            }
        } else {
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state,$message,$url);
    }

    #编辑
    public function edit(){

        $id = (int) I('get.id');
        if (!isset ($id)) {
            $this->error('参数错误');
        }
        $obj = M('Admin');
        $list = $obj->where('id=' . $id)->find();
        $list['city'] = explode(',',$list['city']);
        $this->assign('list', $list);

        $obj = M('City');
        $this->assign('city',$obj->select());
        $obj = M('Role');
        $this->assign('group',$obj->select());

        $this->display();
    }

    #更新
    public function update(){

        $obj = D('Admin');

        if ($data = $obj->create()) {
            if($data['password']==''){
                unset($data['password']);
            }else{
                $data['password']=md5($data['password']);
            }
            if ($obj->save($data)) {

                $role = M('roleUser');
                $role->where('user_id='.$data['id'])->setField('role_id',$data['group']);

                $state = 'SUCCESS';
                $message = '管理员编辑成功';
                $url = U('User/index');
            } else {
                $state = '编辑失败';
            }
        } else {
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state,$message,$url);
    }

    //删除
    public function del() {
        $id = $_GET['id'];
        if (!isset ($id)) {
            echo '0';
        }

        $obj = M('Admin');
        $list = $obj->where('id=' . $id)->delete();
        
        $access = M('AdminLog');
        $access->where('uid=' . $id)->delete();

        $roleUser=M('roleUser');
        $roleUser->where('user_id='.$id)->delete();

        if(!$list){
            echo '0';
        }else{
            echo '1';
        }
    }

    //管理员登陆日志
    function log(){

        $uid = $_GET['id'];

        if($uid!=''){
            $map['uid']=$uid;
        }
        $log = M('AdminLog');
        
        $count = $log->where($map)->count();

        import("@.ORG.Page");

        $page = new Page($count, 20);

        $show = $page->show();

        $list = $log->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();

        foreach ($list as $key => $value) {
            $list[$key]['username'] = M('Admin')->where('id='.$value['uid'])->getField('username');
        }

        $this->assign('list', $list);

        $this->assign('page', $show);

        $this->display();

    }

    //删除日志
    function delog(){

        $id = $_POST['selectedids'];

        if(!isset($id)){
            $state = "您没有选择任何信息！";
        }else{
            $where['id']=array('in',$id);
            $log=M('adminLog');
            $list = $log->where($where)->delete();
            if($list){
                $state = 'SUCCESS';
                $message = '日志删除成功';
                $url = U('User/log',array('id'=>I('post.userID')));
            }else{
                $state = "日志删除失败";
            }
        }        
        echo $this->echo_json_str($state,$message,$url);
    }

    //修改密码
    function password(){
        if(isset($_POST['dosubmit'])) {
            $oldpassword = I('post.oldpwd');
            $password = I('post.password');
            $condition['id']=$this->adminID;
            $obj = M('Admin');
            $userpwd = $obj->where($condition)->field('password')->find();
            if($userpwd['password']!=md5($oldpassword)){
                $state = '原始密码错误';
            }else{
                $data['password']=md5($password);
                $obj->where($condition)->save($data);
                $state = 'SUCCESS';
                $message = '密码修改成功';
                $url = U('Index/index');
            }
            echo $this->echo_json_str($state,$message,$url);            
        }else{
            $this->display();
        }        
    }
}